package com.cmccsi.service.impl;



import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;





import com.cmccsi.entity.qxdeptMemberDto;
import com.cmccsi.mapper.qxdeptMemberDtoMapper;
import com.cmccsi.service.DeptMemberService;
import com.cmccsi.utils.MybatisUtil;

public class DeptMemberServiceImpl implements DeptMemberService {
	
	private SqlSessionFactory sessionFactory = MybatisUtil.getInstance();
	private DeptMemberRedisServiceImpl dptMemberRedis=new DeptMemberRedisServiceImpl();

	@Override
	public Boolean delete(Long id) {
		SqlSession session = sessionFactory.openSession();
		try {
			qxdeptMemberDtoMapper deptMMapper = session.getMapper(qxdeptMemberDtoMapper.class);
			deptMMapper.deleteByPrimaryKey(id);
			session.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public Boolean update(qxdeptMemberDto deptM) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {
			
			qxdeptMemberDtoMapper deptMMapper = session.getMapper(qxdeptMemberDtoMapper.class);
			deptMMapper.updateByPrimaryKey(deptM);
			session.commit();		
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public qxdeptMemberDto find(Long deptId) {
		// TODO Auto-generated method stub
		qxdeptMemberDto deptm = null;
		SqlSession session = sessionFactory.openSession();
		try {
			qxdeptMemberDtoMapper deptMMapper = session.getMapper(qxdeptMemberDtoMapper.class);
			deptm =deptMMapper.selectByPrimaryKey(deptId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return deptm;
	}


	@Override
	public Boolean save(qxdeptMemberDto deptM) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {
			qxdeptMemberDtoMapper deptMMapper = session.getMapper(qxdeptMemberDtoMapper.class);
			deptMMapper.insert(deptM);
			session.commit();
			
			dptMemberRedis.save(deptM);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public Boolean deleteByDeptidAndMemberids(Long Deptid, List<Long> usersids) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {
			qxdeptMemberDtoMapper deptMMapper = session.getMapper(qxdeptMemberDtoMapper.class);
			deptMMapper.deleteByDeptidAndMemberids(Deptid,(Long[]) usersids.toArray());
			session.commit();
			dptMemberRedis.deleteByDeptidAndMemberids(Deptid, usersids);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public Boolean deleteByDeptid(Long deptId) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {
			qxdeptMemberDtoMapper deptMMapper = session.getMapper(qxdeptMemberDtoMapper.class);
		    List<Long> lstMemberids=	deptMMapper.selectMemberIdsByDeptId(deptId);		    
		    deleteByDeptidAndMemberids(deptId,lstMemberids);
			session.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;
	}

}
